package com.amkai.aes.webservice.binary.objecthandler.inventory;

import com.amkai.aes.aescommon.utilities.BeanPropertyHelper;

public class PropertyHQLGeneratorFactory {

	public static PropertyHQLGenerator getGenerator(Class<?> itemClass,
			String instanceName, String propertyName,
			ResolverServiceManager resolverMgr, boolean caseSensitive,
			boolean exactMatch) {
		if (propertyName.contains(".")) {
			return getAdvancedGenerator(itemClass, instanceName, propertyName, resolverMgr, caseSensitive, exactMatch);
		}
		if (propertyName.startsWith("tranzient")) {
			return new TransientPropertyHQLGenerator(instanceName, propertyName, resolverMgr);
		}
		Class<?> propertyType = BeanPropertyHelper.getPropertyType(itemClass, propertyName);
		if (propertyType == null) return null;
		if (propertyType.isAssignableFrom(String.class)) {
			return new StringHQLGenerator(instanceName, propertyName, resolverMgr, caseSensitive, exactMatch);
		} else if (propertyType.isEnum()) {
			return new EnumHQLGenerator(instanceName, propertyName, resolverMgr, propertyType);
		} else if (Long.class.isAssignableFrom(propertyType)) {
			return new LongHQLGenerator(instanceName, propertyName, resolverMgr);
		} else if (Integer.class.isAssignableFrom(propertyType)) {
			return new IntegerHQLGenerator(instanceName, propertyName, resolverMgr);
		} else if (Boolean.class.isAssignableFrom(propertyType) || boolean.class.isAssignableFrom(propertyType)) {
			return new BooleanHQLGenerator(instanceName, propertyName, resolverMgr);
		}
		
		return null;
	}

	private static PropertyHQLGenerator getAdvancedGenerator(
			Class<?> itemClass, String instanceName, String propertyName,
			ResolverServiceManager resolverMgr, boolean caseSensitive,
			boolean exactMatch) {
		AdvancedHQLGenerator gen = new AdvancedHQLGenerator(instanceName, propertyName, resolverMgr);
		gen.initialize(itemClass, caseSensitive, exactMatch);
		return gen;
	}

}
